package com.xl.rentkeeper.net;


import com.xl.fuse.protos.packet.Packet;
import com.xl.fuse.protos.packet.RetCode;

/**
 * Created by zjm on 2015/6/24.
 */
public class Request {
    public Packet packet;
    public long timeOut = 15 * 1000;
    public long queueTs;
    public INetCallback callback;
    public Object obj;
    public Object oriPb;
    public Runnable timeOutRun;

    public Packet getTimeoutPacket() {
        Packet.Builder builder = new Packet.Builder(packet);
        return builder.ret(RetCode.TIMEOUT.getValue()).error("请求超时").build();
    }
    public long getDelay() {
        return timeOut - (System.currentTimeMillis() - queueTs);
    }

    @Override
    public String toString() {
        return dumpPacket(packet);
    }

    public static String dumpPacket(Packet packet) {
        StringBuilder sb = new StringBuilder();
        sb.append("uin=");sb.append(packet.uin);
        sb.append(" cmd=");sb.append(packet.cmd);
        sb.append(" seq=");sb.append(packet.seq);
        sb.append(" ret=");sb.append(packet.ret);
        sb.append(" err=");sb.append(packet.error);
        sb.append(" imei=");sb.append(packet.imei);
        sb.append(" bsize=");sb.append(packet.eBody == null ? 0 : packet.eBody.size());
        return sb.toString();
    }
}
